package cn.bluemobi.app.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Description: <br/>
 * Date: 2015年7月10日 下午5:29:29 <br/>
 * 
 * @author hut
 * @version
 * @see
 */
public class JsonUtils {

    public static String toJson(Object obj) {
        if (obj != null) {
            ObjectMapper mapper = new ObjectMapper();
            try {
                return mapper.writeValueAsString(obj);
            } catch (JsonProcessingException e) {
            }
        }
        return "";
    }

    public static Map<String, Object> jsonStr2Map(String json) {
        if (StringUtils.isNotEmpty(json)) {
            ObjectMapper mapper = new ObjectMapper();
            // 容许没有引号
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            try {
                return mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new HashMap<String, Object>();
    }

    public static void main(String[] args) {
        // String json
        // ="{\"status\":0,\"msg\":\"\",\"code\":\"\",\"data\":{\"list\":[{\"val\":\",2.00-1.80\",\"elementsName\":\"C\",\"id\":1},{\"val\":\"0.10-0.20,0.50-0.40\",\"elementsName\":\"Si\",\"id\":2},{\"val\":\"5.00-6.00,10.00-10.00\",\"elementsName\":\"Mn\",\"id\":3},{\"val\":\"10.00-10.00,20.00-19.00\",\"elementsName\":\"P\",\"id\":4},{\"val\":\"0.20-0.20,0.55-0.55\",\"elementsName\":\"Cr\",\"id\":5},{\"val\":\"1.50-1.50,2.50-2.50\",\"elementsName\":\"Ni\",\"id\":6}],\"steelDetail\":{\"id\":1,\"system\":\"英标\",\"steelId\":null,\"name\":\"420\",\"steelNo\":null,\"yxNo\":\"M400\",\"createTime\":null}}}";
        String json = "{\"list\":[{\"elementsName\":\"C\",\"val\":\",2.00-2.00\",\"id\":1},{\"elementsName\":\"Si\",\"val\":\"0.20-0.20,\",\"id\":2},{\"elementsName\":\"Mn\",\"val\":\"5.00-5.00,10.00-10.00\",\"id\":3},{\"elementsName\":\"P\",\"val\":\"10.00-10.00,20.00-20.00\",\"id\":4},{\"elementsName\":\"Cr\",\"val\":\"0.20-0.20,0.55-0.55\",\"id\":5},{\"elementsName\":\"Ni\",\"val\":\"1.50-1.50,2.50-2.50\",\"id\":6}],\"steelDetail\":{\"id\":\"1\",\"name\":\"X210Cr12\",\"newName\":\"X210Cr1222\",\"system\":\"德标\",\"yxNo\":\"M400\"}}";
        JSONObject j = new JSONObject(json);
        Map<String, Object> map = JsonUtils.jsonStr2Map(json.toString());
        Map<String, Object> steelMap = (Map<String, Object>) map.get("steelDetail");
        List<Map<String, Object>> itemMap = (List<Map<String, Object>>) map.get("list");
        System.out.println(steelMap);
        String id = String.valueOf(steelMap.get("id"));
        String name = String.valueOf(steelMap.get("name"));
        System.out.println(itemMap);
        Map<String, String> saveMap = new HashMap<String, String>();
        for (int i = 0; i < itemMap.size(); i++) {
            int elementId = (Integer) itemMap.get(i).get("id");
            String elementName = (String) itemMap.get(i).get("elementsName");
            String val = (String) itemMap.get(i).get("val");
            String min = val.split(",")[0];
            String max = "";
            if (val.split(",").length > 1) {
                max = val.split(",")[1];
            }
            System.out.println(elementId + "====" + elementName + "====" + min + "====" + max);
            saveMap.put(elementId + "_" + elementName, min + "," + max);
            // 有最大值与最小值
            if (!ValidUtil.isEmpty(min) && !ValidUtil.isEmpty(max)) {
                String oldMin = min.split("-")[0];
                String newMin = min.split("-")[1];
                String oldMax = max.split("-")[0];
                String newMax = max.split("-")[1];
                if (oldMin.equals(newMin) && oldMax.equals(newMax)) {
                    saveMap.remove(elementId + "_" + elementName);
                }
            }
            // 只有一个值
            else if (!ValidUtil.isEmpty(min)) {
                String oldMin = min.split("-")[0];
                String newMin = min.split("-")[1];
                if (oldMin.equals(newMin)) {
                    saveMap.remove(elementId + "_" + elementName);
                }
            } else if (!ValidUtil.isEmpty(max)) {
                String oldMax = max.split("-")[0];
                String newMax = max.split("-")[1];
                if (oldMax.equals(newMax)) {
                    saveMap.remove(elementId + "_" + elementName);
                }
            }
        }
        for (String key : saveMap.keySet()) {
            // System.out.println(key+"*********"+saveMap.get(key));
            String defaultDetailId = key.split("_")[0];
            String elementsName = key.split("_")[1];
            String min = saveMap.get(key).split(",")[0];
            String max = "";
            if (saveMap.get(key).split(",").length > 1) {
                max = saveMap.get(key).split(",")[1];
            }
            float defaultMinValue = 0;
            float minValue = 0;
            float defaultMaxValue = 0;
            float maxValue = 0;
            if (!ValidUtil.isEmpty(min)) {
                defaultMinValue = Float.parseFloat(min.split("-")[0]);
                minValue = Float.parseFloat(min.split("-")[1]);
            }
            if (!ValidUtil.isEmpty(max)) {
                defaultMaxValue = Float.parseFloat(max.split("-")[0]);
                maxValue = Float.parseFloat(max.split("-")[1]);
            }

        }

    }
}
